package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.dialog.PayWayDialog;
import com.ckd.flyingtrip.javabean.CarBean;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 * 购买电动车
 * */
public class GoumaiActivity extends BaseActivity {
    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.txt_zhidaojia)
    TextView txtZhidaojia;
    @BindView(R.id.txt_shengyu)
    TextView txtShengyu;
    @BindView(R.id.txt_huodong)
    TextView txtHuodong;
    @BindView(R.id.btn_res)
    TextView btnRes;
    @BindView(R.id.img_che)
    ImageView imgChe;
    @BindView(R.id.txt_yajin)
    TextView txtYajin;
    @BindView(R.id.txt_zujin)
    TextView txtZujin;
    @BindView(R.id.lin_zhuche)
    LinearLayout linZhuche;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goumai);
        ButterKnife.bind(this);
        Tools.title_info(this, "车辆详情");
        id = getIntent().getExtras().getString("id");
        volley(id);
    }

    private PayWayDialog.Builder pb;


    //车详细
    public void volley(String id) {
        final Gson mGson = new Gson();
        String url = Constants.CARXIANGXI;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("车详细", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        String data = json.getString("data");
                        CarBean carBean = mGson.fromJson(data, CarBean.class);
                        Tools.loadImg(carBean.getBicyclePictureUrl(), imgChe);
                        txtZhidaojia.setText("厂家指导价:" + carBean.getBicycle_price());
                        if (carBean.getUsed_type().equals("0")) {
                            linZhuche.setVisibility(View.VISIBLE);
                            txtYajin.setText("押金:"+carBean.getBicycle_deposit()+"/月");
                            txtZujin.setText("租金:"+carBean.getBicycle_rent()+"/月");
                            btnRes.setText("立即租用");
                            btnRes.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent(GoumaiActivity.this, MyZhanghuActivity.class);
                                    intent.putExtra("id", carBean.getBicycle_id());
                                    intent.putExtra("code", carBean.getBicycle_code());
                                    startActivity(intent);
                                }
                            });
                        } else {
                            btnRes.setText("立即购买");
                            btnRes.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    pb = new PayWayDialog.Builder(GoumaiActivity.this,
                                            new PayWayDialog.Builder.PriorityListener() {
                                                @Override
                                                public void refreshPriorityUI(String f) {
                                                    volleyQueryFei(id, f);
                                                    pb.create().dismiss();
                                                }
                                            });
                                    pb.create().show();
                                }
                            });
                        }
                        txtShengyu.setText("剩余:" + carBean.getBicycle_surplus());

                    } else {
                        Log.i("站好", "账号");
                    }
//                    toast(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("bicycleId", id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }


    /*
     * 查询费用 准备掉起支付
     * */
    public void volleyQueryFei(String id, String type) {
        if (type.equals("weixin")) {
//            Weixin weixin = new Weixin(GoumaiActivity.this, "");
//            weixin.volleyDINGDAN(id);
            dismisProgressDialog();
        } else if (type.equals("alipay")) {
            createLoadingDialog(this, "正在支付中...", true);
            Alipay alipay = new Alipay(GoumaiActivity.this, "");
            alipay.volleyQueryZhifu(id);
        }
    }
}
